package cn.itrip.auth.service;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Set;

/**
 * @author Super God Jie
 * @version 1.0  2022-05-01 09:48
 */
@Service
public class MailANDSMSService {

	@Resource
	private JavaMailSender javaMailSender;

	public void sendMail(String userCode,String activeCode) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom("XXXXXXXX@163.com");
		message.setSubject("欢迎光临");
		message.setText("激活码："+activeCode);
		message.setTo(userCode);
		javaMailSender.send(message);
	}

	public void sendPhone(String userCode, String activeCode) {
		//生产环境请求地址：app.cloopen.com
		String serverIp = "app.cloopen.com";
		//请求端口
		String serverPort = "8883";

		//请使用管理控制台中已创建应用的APPID
		String appId = "";
		CCPRestSmsSDK sdk = new CCPRestSmsSDK();
		sdk.init(serverIp, serverPort);
		//主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
		sdk.setAccount("你的accountSid", "你的accountToken");
		sdk.setAppId(appId);
		sdk.setBodyType(BodyType.Type_JSON);
		String to = userCode;
		String templateId= "1";
		String[] datas = {activeCode};
		HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
		if("000000".equals(result.get("statusCode"))){
			//正常返回输出data包体信息（map）
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for(String key:keySet){
				Object object = data.get(key);
				System.out.println(key +" = "+object);
			}
		}else{
			//异常返回输出错误码和错误信息
			System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
		}
	}
}
